import numpy as np
from scipy.sparse import coo_matrix
#1、coo的表示
_row = np.array([0,3,1,0])
_col = np.array([0,3,1,2])
_data= np.array([4,5,7,9])
coo = coo_matrix((_data,(_row,_col)),shape=(4,4),dtype=np.int)
coo.todense()
coo.toarray()

#2、csr的表示法
import numpy as np
from scipy.sparse import csr_matrix

indptr = np.array([0,2,3,6])
indices = np.array([0,2,2,0,1,2])
data= np.array([1,2,3,4,5,6])
csr = csr_matrix((data,indices,indptr),shape=(3,3))
print(csr.toarray())

A=np.array([[1,0,0,1,0,0],[0,0,2,0,0,1],[0,0,0,2,0,0]])
print(A)
S=csr_matrix(A)
print(S)
print(S.todense())

sparsity = 1.0-np.count_nonzero(A)/A.size

#3、矩阵运算
A  = np.array([[1,0,2,0],[0,0,0,0],[3,0,0,0],[1,0,0,4]])
AS =csr_matrix(A)
b = np.array([1,2,3,4])
c = AS.dot(b)
print(A)
print(b)
print(c)
print(AS)
c2 = AS.dot(AS)
c3 = np.dot(AS,b)
print(c2)
print(c3)

#4、加载.mat文件
import scipy.io as scio

dataFile = 'E://data.mat'
data = scio.loadmat(dataFile)

dataNew = 'E://dataNew.mat'
scio.savemat(dataNew,{'A':data['A']})

